//
//  QSWorkStationHeaderView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/19.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSWorkStationHeaderView.h"
#import "QSPhotoFileCell.h"
#import "QSOfficeFileCellTableViewCell.h"
#import "QSWebViewController.h"
@interface QSWorkStationHeaderView ()
@property (nonatomic, strong)NSArray *dataArray;
@end
static NSString *officeFileCell = @"OfficeFileCell";
static NSString *photoFileCell = @"PhotoFileCell";
@implementation QSWorkStationHeaderView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.isOpen = NO;//默认关闭状态
        [self configSubView];
    }
    return self;
}

- (void)configSubView{
    
    [self addSubview:self.whiteBackGroundView];
    [self.whiteBackGroundView addSubview:self.titleLabel];
    [self.whiteBackGroundView addSubview:self.sendPeopleLabel];
    [self.whiteBackGroundView addSubview:self.sendDateLabel];
    [self.whiteBackGroundView addSubview:self.contentLabel];
    [self.whiteBackGroundView addSubview:self.openBackgroudView];
    
    [self.openBackgroudView addSubview:self.accessoryLabel];
    [self.openBackgroudView addSubview:self.accessoryImageView];
    [self.whiteBackGroundView addSubview:self.closeBackGroudView];
    [self.closeBackGroudView addSubview:self.defaultImageView];
    [self.closeBackGroudView addSubview:self.numLabel];
    [self.whiteBackGroundView addSubview:self.tableView];
    [self addSubview:self.bottomClearColorView];
    [_whiteBackGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kRealValue(10));
        make.left.equalTo(self).offset(kRealValue(10));
        make.right.equalTo(self).offset(- kRealValue(10));
//        make.bottom.equalTo(self).offset(-kRealValue(10));
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBackGroundView).offset(kRealValue(10));
        make.right.equalTo(self.whiteBackGroundView).offset(- kRealValue(10));
        make.top.equalTo(self.whiteBackGroundView).offset(kRealValue(10));
    }];
    [_sendPeopleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(kRealValue(15));
        make.left.equalTo(self.whiteBackGroundView).offset(kRealValue(10));
    }];
    [_sendDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(kRealValue(15));
        make.right.equalTo(self.whiteBackGroundView).offset(-kRealValue(10));
    }];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBackGroundView).offset(kRealValue(10));
        make.right.equalTo(self.whiteBackGroundView).offset(-kRealValue(10));
        make.top.equalTo(self.sendPeopleLabel.mas_bottom).offset(kRealValue(15));
    }];
    [_openBackgroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.whiteBackGroundView);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(kRealValue(25));
        make.height.offset(kRealValue(30));
    }];
    [_accessoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.openBackgroudView).offset(kRealValue(10));
        make.centerY.equalTo(self.openBackgroudView);
    }];
    [_accessoryImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.openBackgroudView);
        make.right.equalTo(self.openBackgroudView).offset(- kRealValue(10));
    }];
    [_closeBackGroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBackGroundView).offset(kRealValue(10));
        make.right.equalTo(self.whiteBackGroundView).offset(-10);
        make.top.equalTo(self.openBackgroudView.mas_bottom).offset(0);
        make.height.offset(kRealValue(30));
    }];
    [_defaultImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.closeBackGroudView).offset(kRealValue(7));
        make.centerY.equalTo(self.closeBackGroudView);
        make.width.height.offset(kRealValue(12));
    }];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.defaultImageView.mas_right).offset(kRealValue(10));
        make.centerY.equalTo(self.closeBackGroudView);
    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.openBackgroudView.mas_bottom).offset(0);
        make.left.right.equalTo(self.whiteBackGroundView);
//        make.height.offset(kRealValue(20));
        make.bottom.equalTo(self.whiteBackGroundView).offset(-kRealValue(5));
    }];
    [_bottomClearColorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.whiteBackGroundView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.offset(kRealValue(10));
        make.bottom.equalTo(self);
    }];
    [self updateHeaderViewFrame];

}
#pragma mark - Lazy load
- (UIView *)whiteBackGroundView{
    if (!_whiteBackGroundView) {
        _whiteBackGroundView = [[UIView alloc]init];
        _whiteBackGroundView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _whiteBackGroundView.layer.cornerRadius = 5;
    }
    return _whiteBackGroundView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithName:@"标题" font:[UIFont QSFontOfSize16] textColor:[UIColor QSColorBlack434343] textAlignment:NSTextAlignmentLeft];
        _titleLabel.numberOfLines = 2;
        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    }
    return _titleLabel;
}
-(UILabel *)sendPeopleLabel{
    if (!_sendPeopleLabel) {
        _sendPeopleLabel = [UILabel labelWithName:@"发起人" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorGray999999] textAlignment:NSTextAlignmentLeft];
    }
    return _sendPeopleLabel;
}

- (UILabel *)sendDateLabel{
    if (!_sendDateLabel) {
        _sendDateLabel = [UILabel labelWithName:@"发起时间" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorGray999999] textAlignment:NSTextAlignmentLeft];
    }
    return _sendDateLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [UILabel labelWithName:@"内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长内容可能会很长" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorGray999999] textAlignment:NSTextAlignmentLeft];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UIView *)openBackgroudView{
    if (!_openBackgroudView) {
        _openBackgroudView = [[UIView alloc]init];
//        _openBackgroudView.backgroundColor = [UIColor cyanColor];
        _openBackgroudView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleOpenList:)];
        [_openBackgroudView addGestureRecognizer:tap];
    }
    return _openBackgroudView;
}

- (UILabel *)accessoryLabel{
    if (!_accessoryLabel) {
        _accessoryLabel = [UILabel labelWithName:@"附件：" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorGray999999] textAlignment:NSTextAlignmentLeft];
    }
    return _accessoryLabel;
}

- (UIImageView *)accessoryImageView{
    if (!_accessoryImageView) {
        _accessoryImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_selectpeople"]];
    }
    return _accessoryImageView;
}

- (UIView *)closeBackGroudView{
    if (!_closeBackGroudView) {
        _closeBackGroudView = [[UIView alloc]init];
        _closeBackGroudView.layer.cornerRadius = 5;
        _closeBackGroudView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
        _closeBackGroudView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleOpenList:)];
        [_closeBackGroudView addGestureRecognizer:tap];
    }
    return _closeBackGroudView;
}

- (UIImageView *)defaultImageView{
    if (!_defaultImageView) {
        _defaultImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_visit_tanhao"]];
    }
    return _defaultImageView;
}

- (UILabel *)numLabel{
    if (!_numLabel) {
        _numLabel = [UILabel labelWithName:@"" font:[UIFont QSFontOfSize12] textColor:[UIColor QSColorBlack666666] textAlignment:NSTextAlignmentLeft];
    }
    return _numLabel;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//        _tableView.backgroundColor = [UIColor greenColor];
        _tableView.layer.cornerRadius = 5;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"QSOfficeFileCellTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:officeFileCell];
        [_tableView registerNib:[UINib nibWithNibName:@"QSPhotoFileCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:photoFileCell];
//        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:officeFileCell];
    }
    return _tableView;
}

- (UIView *)bottomClearColorView{
    if (!_bottomClearColorView) {
        _bottomClearColorView = [[UIView alloc]init];
        _bottomClearColorView.backgroundColor = [UIColor clearColor];
    }
    return _bottomClearColorView;
}

#pragma mark - Action
- (void)handleOpenList:(UITapGestureRecognizer *)tap{
//    [self updateHeaderViewFrame];
    _isOpen = !_isOpen;
    if (self.didOpenFileListBlock) {
        self.didOpenFileListBlock(_isOpen);
    }
}

#pragma mark -- TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    QSWorkStationFileModel *model = self.dataArray[indexPath.row];
    if ([model.ewFileType isEqualToString:@"photo"] || [model.ewFileType isEqualToString:@"other"]) {
        return kRealValue(64);
    }else{
        return kRealValue(50);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QSWorkStationFileModel *model = self.dataArray[indexPath.row];
    
    if ([model.ewFileType isEqualToString:@"photo"] || [model.ewFileType isEqualToString:@"other"]) {
        QSPhotoFileCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:photoFileCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.fileModel = model;
        return cell;
    }else{
        QSOfficeFileCellTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:officeFileCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.fileModel = model;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"四海");
    QSWorkStationFileModel *model = self.dataArray[indexPath.row];
    QSWebViewController *web = [[QSWebViewController alloc] init];
    web.URLString = model.ewFileUrl;
    web.hideNavigationBar = NO;
    [[UIViewController currentViewController].navigationController pushViewController:web animated:YES];
}

- (void)updateHeaderViewFrame{
    self.closeBackGroudView.hidden = self.isOpen;
    self.tableView.hidden = !self.isOpen;
}


- (void)setModel:(QSWorkDetailsModel *)model{
    _model = model;
    self.titleLabel.text = model.ewTitle;
    self.contentLabel.text = model.ewContent;
    self.sendPeopleLabel.text = [NSString stringWithFormat:@"发起人：%@", model.ewCreateUserName];
    self.sendDateLabel.text = [NSString stringWithFormat:@"发起时间：%@", [NSDate dateWithTimestamp:model.ewCreateTime format:@"MM-dd HH:mm"]];
    self.numLabel.text = [NSString stringWithFormat:@"共 %lu 个附件", (unsigned long)model.workFileList.count];
    NSInteger lenth = [NSString stringWithFormat:@"%ld", model.workFileList.count].length;
    [self.numLabel setAttributedText:[UILabel setLabelTextColorWithString:self.numLabel.text Range:NSMakeRange(2, lenth) andColor:[UIColor redColor]]];
    
    self.dataArray = model.workFileList;
    [self.tableView reloadData];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
